package com.liu.test.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liu.test.common.BaseContext;
import com.liu.test.common.ObjectRestResponse;
import com.liu.test.dto.GoodWithNum;
import com.liu.test.dto.GoodWithPrice;
import com.liu.test.entity.LdrFinalOrder;
import com.liu.test.entity.LdrGoods;
import com.liu.test.entity.LdrOrder;
import com.liu.test.entity.LdrShoppingCar;
import com.liu.test.service.LdrFinalOrderService;
import com.liu.test.service.LdrGoodsService;
import com.liu.test.service.LdrOrderService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping
@CrossOrigin
public class FinalOrderController {
    @Autowired
    private LdrOrderService orderService;

    @Autowired
    private LdrFinalOrderService finalOrderService;

    @Autowired
    private LdrGoodsService goodsService;

    //获取订单信息
    @GetMapping("/getOrder")
    public ObjectRestResponse<List<LdrFinalOrder>> getOrder() {
        String userId = "123456789";//BaseContext.getUserId();
        LambdaQueryWrapper<LdrOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LdrOrder::getUserId, userId);

        List<LdrOrder> ldrOrderList = orderService.list(queryWrapper);
        HashSet<String> orderId = new HashSet<>();
        for (int i = 0; i < ldrOrderList.size(); i++) {
            orderId.add(ldrOrderList.get(i).getOrderId());
        }

        List<LdrFinalOrder> ldrFinalOrderList = orderId.stream().map(item -> {
            LdrFinalOrder finalOrder;
            LambdaQueryWrapper<LdrFinalOrder> finalOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
            finalOrderLambdaQueryWrapper.eq(LdrFinalOrder::getFinalOrderId, item);
            finalOrder = finalOrderService.getOne(finalOrderLambdaQueryWrapper);
            return finalOrder;
        }).collect(Collectors.toList());
        ObjectRestResponse<List<LdrFinalOrder>> result = new ObjectRestResponse<>();
        result.data(ldrFinalOrderList).message("订单信息");
        return result;
    }

    //显示订单详情
    @PostMapping("/showOrderInfo")
    public ObjectRestResponse<GoodWithPrice> showOrderInfo(@RequestBody LdrFinalOrder finalOrder) {
        String userId = "123456789";//BaseContext.getUserId();
        String finalOrderId = finalOrder.getFinalOrderId();
        LambdaQueryWrapper<LdrOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LdrOrder::getOrderId, finalOrderId).eq(LdrOrder::getUserId, userId);
        List<LdrOrder> ldrOrderList = orderService.list(queryWrapper);
        List<GoodWithNum> goodWithNumList = ldrOrderList.stream().map(item -> {
            GoodWithNum goodWithNum = new GoodWithNum();

            LdrGoods goods;
            LambdaQueryWrapper<LdrGoods> goodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            goodsLambdaQueryWrapper.eq(LdrGoods::getGoodId, item.getGoodId());
            goods = goodsService.getOne(goodsLambdaQueryWrapper);
            goodWithNum.setLdrGoods(goods);
            goodWithNum.setNum(item.getNumber());

            return goodWithNum;
        }).collect(Collectors.toList());
        GoodWithPrice goodWithPrice = new GoodWithPrice();
        goodWithPrice.setGoodWithNumList(goodWithNumList);
        double sumOfPrice = 0.0;
        for(GoodWithNum goodWithNum : goodWithNumList){
            sumOfPrice += goodWithNum.getNum() * goodWithNum.getLdrGoods().getGoodPrice();
        }
        goodWithPrice.setSumOfPrice(sumOfPrice);
        ObjectRestResponse<GoodWithPrice> result = new ObjectRestResponse<>();
        result.data(goodWithPrice).message("订单详情");
        return result;
    }

    //删除订单
    @PostMapping("/deleteOrder")
    public ObjectRestResponse<LdrFinalOrder> deleteOrder(@RequestBody LdrFinalOrder ldrFinalOrder) {
        String finalOrderId = ldrFinalOrder.getFinalOrderId();
        LambdaQueryWrapper<LdrFinalOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LdrFinalOrder::getFinalOrderId, finalOrderId);
        //删除订单
        boolean remove = finalOrderService.remove(queryWrapper);
        LambdaQueryWrapper<LdrOrder> ldrOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ldrOrderLambdaQueryWrapper.eq(LdrOrder::getOrderId, finalOrderId);
        //删除订单有关的商品信息
        boolean remove1 = orderService.remove(ldrOrderLambdaQueryWrapper);
        ObjectRestResponse<LdrFinalOrder> result = new ObjectRestResponse<>();
        result.data(ldrFinalOrder);
        result = (remove && remove1) ? result.message("删除成功") : result.message("删除失败");
        return result;
    }
}
